let maydan;
let senk;
let stap = 20;
let tamer;
let arrow = 38;
let srec=0;
let sreclbl;
let runndig = false;
window.onload = function () {
    maydan = document.getElementById('maydan');
    sreclbl=document.getElementById('srec');
    let eggs = document.getElementsByClassName('senk');
    senk = [];
    for (let i = 0; i < eggs.length; i++) {
        let x = parseInt(eggs[i].style.left);
        let y = parseInt(eggs[i].style.top);
        senk.push({x: x, y: y});
    }
};
window.onkeydown = function () {
    let key = event.keyCode;
    if (key === 37) {
        if (arrow !==  39) {
            arrow = key;
        }
    }
    if (key === 38) {
        if (arrow !== 40) {
            arrow = key;
        }
    }
    if (key === 39) {
        if (arrow !== 37) {
            arrow = key;
        }
    }
    if (key === 40) {
        if (arrow !== 38) {
            arrow = key;
        }
    }
    if (key === 32) {
        if (!runndig) fun(senk);
    }
};
function fun(senk, cls = 'senk') {
    randomCreateEgg();
    runndig = true;
    tamer = setInterval(function () {
        let eggs = document.getElementsByClassName(cls);
        let x = parseInt(eggs[0].style.left);
        let y = parseInt(eggs[0].style.top);
        if (arrow === 37) {
            x -= stap;
            if (x < 0) gamrOver();
            let tesElm = document.elementFromPoint(x + 1, y +10);
            if (tesElm.className === 'egg') {
            eggtrigger(tesElm);
            }else if(tesElm.className==="senk") {
                gamrOver();
            }
        }
        if (arrow === 38) {
            y -= stap;
            if (y < 0) gamrOver();
            let tesElm = document.elementFromPoint(x +10, y + 1);
            if (tesElm.className === 'egg'){
            eggtrigger(tesElm);
            }else if(tesElm.className==="senk") {
                gamrOver();
            }
        }
        if (arrow === 39) {
            x += stap;
            if (x > 500) gamrOver();
            let tesElm = document.elementFromPoint(x + 19, y + 10);
            if (tesElm.className === 'egg'){
            eggtrigger(tesElm);
            }else if(tesElm.className==="senk") {
                gamrOver();
            }
        }
        if (arrow === 40) {
            y += stap;
            if (y > 500) gamrOver();
            let tesElm = document.elementFromPoint(x + 10, y + 19);
            if (tesElm.className === 'egg'){
            eggtrigger(tesElm);
            }else if(tesElm.classNam==="senk"){
                gamrOver();
            }
        }
        for (let i = 0; i < eggs.length; i++) {
            senk[i].x = parseInt(eggs[i].style.left);
            senk[i].y = parseInt(eggs[i].style.top);
            if (i === 0) {
                eggs[i].style.left = x + "px";
                eggs[i].style.top = y + "px";
            } else {
                eggs[i].style.left = senk[i - 1].x + "px";
                eggs[i].style.top = senk[i - 1].y + "px";
            }
        }
    }, 100)
}
function randomCreateEgg() {
    let x = Math.floor(Math.random() * 25) * 20;
    let y = Math.floor(Math.random() * 25) * 20;
    let egg = document.createElement("div");
    egg.style.left = x + "px";
    egg.style.top = y + "px";
    egg.className = "egg";
    maydan.appendChild(egg);
}
function eggtrigger(egg) {
    egg.remove();
    srec++;
    sreclbl.innerHTML=srec;
    let tap = document.createElement('div');
    tap.style.left = [senk.length - 1].x + 'px';
    tap.style.top = [senk.length - 1].y + 'px';
    tap.className = 'senk';
    senk.push({x: senk[senk.length - 1].x, y: senk[senk.length - 1].y});
    maydan.appendChild(tap);
    randomCreateEgg();
}
function gamrOver() {
    setInterval(tamer);
    alert('game Over');
    location.reload(1);
}